home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / SeqPups / appsrc / autoseq.src / RInlines.H < prev    next >
Text File  |  1996-07-05  |  4KB  |  200 lines

  1. //    ============================================================================
  2. //    RInlines.H                                                        80 spaces
  3. //    Reece Hart, reece@ibc.wustl.edu                                    tab=4 spaces
  4. //    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
  5. //    This file contains homegrown inline functions which are used in    many of my
  6. //    sources.
  7. //    ========================================|===================================
  8.  
  9. #ifndef _H_RInlines                            // include this file only once
  10. #define _H_RInlines
  11.  
  12. #include    <stddef.h>
  13. #include    <math.h>
  14. #include    "RInclude.H"
  15.  
  16. #ifndef WIN_MAC  // dgg 
  17. const double _PI = 3.14159265358979;
  18. #endif
  19.  
  20. //
  21. //    Swap
  22. //    swap the values of any two elements
  23. //
  24. template<class T>
  25. inline
  26. void
  27. Swap(T& elem1, T& elem2)
  28.     { T dummy=elem2; elem2=elem1; elem1=dummy; }
  29.  
  30. //
  31. //    `Pluralizer' -- return "s" if number != 1
  32. //
  33. template<class T>
  34. inline
  35. char*
  36. Plural(T number)
  37.     { return ((number==1)?"":"s"); }
  38.  
  39. //
  40. //    return "yes" or "no"
  41. //
  42. inline
  43. char*
  44. YesNo(bool b)
  45.     { return (b==TRUE?"yes":"no"); }
  46.  
  47. //
  48. //    return "true" or "false"
  49. //
  50. inline
  51. char*
  52. TrueFalse(bool b)
  53.     { return (b==TRUE?"true":"false"); }
  54.  
  55. //
  56. //    tag with a character if true
  57. //    by default, * if true, space if false
  58. //
  59. inline
  60. char
  61. Tag(bool b,char Ttag='*',char Ftag=' ')
  62.     { return (b==TRUE?Ttag:Ftag); }
  63.  
  64.  
  65. //
  66. //    simple interpolation/extrapolation function
  67. //
  68. inline
  69. double
  70. Interpolate(double x1, double y1, double x2, double y2, double at_x)
  71.     {
  72.     return y1 + (y2-y1)/(x2-x1)*(at_x-x1);
  73.     }
  74.  
  75. //
  76. //    Exponential Decay
  77. //        x0 * exp(-k*i)
  78. //
  79. inline
  80. double
  81. ExpDecay(double x0, double k, double i)
  82.     {
  83.     return    x0 * exp(-k*i);
  84.     }
  85.  
  86. //
  87. //    Z
  88. //    Compute standard z score for a sample
  89. //
  90. inline
  91. double
  92. Z(double obs, double mean, double stddev)
  93.     {
  94.     return (obs-mean)/stddev;
  95.     }
  96.  
  97. //
  98. //    StdNormal
  99. //    Compute probability density for a Z score
  100. //
  101. inline
  102. double
  103. StdNormal(double z)
  104.     {
  105.     return exp(-pow(z,2)/2) / sqrt(2*_PI);
  106.     }
  107.  
  108. //
  109. //    Gaussian distribution using std deviation
  110. //
  111. inline
  112. double
  113. GaussianSD(double obs, double mean, double stddev)
  114.     {
  115.     return exp(-pow((obs-mean)/stddev,2)/2) / (stddev*sqrt(2*_PI));
  116.     }
  117.  
  118. //
  119. //    Gaussian distribution using variance
  120. //
  121. inline
  122. double
  123. GaussianV(double obs, double mean, double var)
  124.     {
  125.     return exp(-pow(obs-mean,2)/2/var) / (sqrt(var*2*_PI));
  126.     }
  127.  
  128. //
  129. //    Pack 4 characters into a single ulong (4:1 compression)
  130. //
  131. inline
  132. long
  133. Pack4Chars(char* c)
  134.     { return ((long) (((((c[0]<<8)+c[1])<<8)+c[2])<<8)+c[3]); }
  135.  
  136. //
  137. //    Unpack the 4 characters from the ulong.
  138. //
  139. inline
  140. char*
  141. Unpack4Chars(long packed, char* chars)
  142.     {
  143.     chars[0] = (char)(packed>>24) & 0xFF;
  144.     chars[1] = (char)(packed>>16) & 0xFF;
  145.     chars[2] = (char)(packed>>8)  & 0xFF;
  146.     chars[3] = (char)(packed)     & 0xFF;
  147.     return chars;
  148.     }
  149.  
  150. //
  151. // Invert
  152. //   Inverts an array of arbitrary objects in memory (in place).
  153. //
  154. // Algorithm:
  155. // 1. initialize left and right pointers to the extreme ends of the array
  156. // 2. while (right > left)
  157. //      swap *left and *right
  158. //        increment left pointer
  159. //        decrement right pointer
  160. //       elihw
  161. // Pointers to objects were chosen over indices in an array to avoid
  162. // the pointer arithmetic involved in array addressing, which I've assumed
  163. // to be greater than the simple add/subtract mechanism for the pointers below.
  164. //
  165. template<class T>
  166. inline
  167. void
  168. Invert(T* arrayPtr, size_t len)
  169.     {
  170.     register T*    left = arrayPtr;            // 'left' ptr; init to 1st T
  171.     register T*    right = arrayPtr + len - 1;    // 'right' ptr; init to last T
  172.  
  173.     while (right-left > 0)
  174.         {
  175.         T temp = *left;
  176.         *left = *right;
  177.         *right = temp;
  178.  
  179.         left++;
  180.         right--;
  181.         }
  182.     }
  183.  
  184. //
  185. // FlipEndian
  186. //      BIGENDIAN <-> LILENDIAN converter
  187. // Assumes that sizeof(T)%8 == 0.  
  188. //template<class T>
  189. //T
  190. //FlipEndian(T item)
  191. //    {
  192. //    Invert((byte*)&item,sizeof(T)/8);
  193. //    return item;
  194. //
  195. //    }
  196. //
  197.  
  198. #endif                                        // conditional inclusion
  199.  
  200.